<!-- 结算入口 -->
<template>
  <div>
    <el-header v-if="this.paramsObj.workItemId" height="36px">
      <header-title :title="headerTitle" />
    </el-header>
    <!-- 项目基础信息 -->
    <ProjectBasicInfo :prj-id="paramsObj.prjId" :prj-type="paramsObj.prjType" />
    <!-- 站址信息 -->
    <ProjectSiteInfo v-if="isShowAttachMent" :prj-id="paramsObj.prjId" />
    <!-- 项目专属信息 -->
    <ProjectExclusiveinfo :prj-id="paramsObj.prjId" :prj-type="paramsObj.prjType" />
    <!-- 商合采购订单-->
    <PuRchaseOrder v-if="isTag" ref="saveList" :params-obj="paramsObj" @showBtnNext="showBtnNext" :prjDetails="prjDetails"/>
    <!-- 附件  -->
    <ProjectAttachmentInfo v-if="isShowAttachMent" :params-obj="attachmentInfo" />
    <!-- 项目审批信息 -->
    <ProjectApprovalInfo v-if="isShowAttachMent" :process-inst-id="Number(paramsObj.processInstId)" />
    <!-- 按钮提交流程 -->
    <ButtonProcess
      v-if="isShowType"
      ref="btnProcess"
      :params-obj="paramsObj"
      :next-btn-dilog-show="nextBtnDilogShow"
      :is-save-true="isSaveTrue"
      @nextSave="nextSave"
      :attachmentInfo="attachmentInfo"
    />
  </div>
</template>

<script>
import HeaderTitle from '@/components/basic/HeaderTitle'
import ProjectBasicInfo from '@/components/jn/JnPrjDetail.vue'
import ProjectSiteInfo from '@/components/jn/JnSiteDetail'
import ProjectExclusiveinfo from '@/components/jn/JnProprietaryDetail'
import ProjectApprovalInfo from '@/components/jn/JnTaskDetail'
import ProjectAttachmentInfo from './projectInfo/projectAttachmentInfo.vue'
import PuRchaseOrder from './purchaseOrder/PurchaseOrder.vue'
import ButtonProcess from './purchaseOrder/ButtonProcess.vue'
import { getWFWorkItem } from '@/api/actualBudget/index'
import { getQueryObject } from '@/utils'
import { getPrjDetail } from '@/api/actualBudget'
import {
  getOnlyId
} from '@/api/settlement/balanceSettlement.js'
export default {
  name: 'Balance',
  components: {
    PuRchaseOrder,
    ButtonProcess,
    ProjectBasicInfo,
    ProjectSiteInfo,
    ProjectExclusiveinfo,
    ProjectApprovalInfo,
    ProjectAttachmentInfo,
    HeaderTitle
  },
  data() {
    return {
      // 顶部标题名字
      headerTitle: '',
      // 是否展示组件
      isTag: false,
      // 是否显示下一步 退回 转派按钮
      isShowType: true,
      // 浏览器解析出来需要的字段
      paramsObj: {
        prjId: '', // 浏览器地址栏获取12755001
        adjustId: '', // 浏览器地址栏获取30013512
        workItemId: '', // 浏览器地址栏获取
        processInstId: '', // 需要接口获取
        titles: '',
        prjType: '',
        type: ''// 浏览器地址栏获取
      },
      // 给附件传递的浏览器参数
      attachmentInfo: {
        prjId: '',
        nodeId: '',
        prjStage: 'Q',
        btnConfig: 'A', // 默认传A .综合查询传B// btnConfig:  A:默认，B：下载附件  C：默认+全览  D:下载附件+全览  E：默认+全览+数字图纸   F：补传
        isChange: '0', // 默认传0
        prjType: '',
        buildModes: '',
        towerType: '0',
        roomType: '0',
        orgId:'',
        projectId:'',
        isNewbusiness:''
      },
      nextBtnDilogShow: false, // 是否让弹框下一步显示
      isShowAttachMent: false,
      prjDetails:{}
    }
  },
  created() {
    this.getBalanceInfo()
    sessionStorage.removeItem('recordIndex')
    sessionStorage.removeItem('rocordId')
  },
  methods: {
    // 判断是否真正保存了
    isSaveTrue() {
      const { materiaList, servierList, _activeName } = this.$refs.saveList.childrenSave()
      if (_activeName === '物资明细信息') {
         return this.changeMaterialSaveListBackStatus(materiaList)
      } else {
        return this.changeServerSaveListBackStatus(servierList)
      }
    },
    changeMaterialSaveListBackStatus(data) {
      const materialStatus = data?.some(item => (+item.status) === 1)
      return materialStatus
    },
    changeServerSaveListBackStatus(data) {
      const backStatus = data?.some(item => {
        return item.childNodeList.some(_item => (+_item.status) === 1)
      })
      return backStatus
    },
    // 下一步保存的时候判断
    nextSave() {
      this.$refs.saveList.nextSaveValidate()
    },
    // 验证以后让 弹框显示
    showBtnNext(tag) {
      // 让弹框显示或隐藏
      this.nextBtnDilogShow = tag
      if (tag) {
        // 如果保存了 则重新打开弹框
        this.$refs.btnProcess.handleClick('doNext')
      }
    },
    // 给附件传递的信息
    async getAttachmentInfo() {
      if (!this.paramsObj.prjId) {
        return
      }
      const { code, data } = await getPrjDetail({ prjId: this.paramsObj.prjId })
      if (+code === 200) {
        this.prjDetails =data
        this.headerTitle = data?.prjName
        this.paramsObj.titles = data?.prjName
        this.paramsObj.prjType = data.prjType
        this.attachmentInfo.prjId = this.paramsObj.prjId
        this.attachmentInfo.prjType = data.prjType
        this.attachmentInfo.buildModes = data.buildModes
        this.attachmentInfo.orgId = data?.orgId
        this.attachmentInfo.projectId = this.paramsObj.prjId
        this.attachmentInfo.isNewbusiness = data?.isNewbusiness
        this.isShowAttachMent = true
        // 代办或者已办进来的
        if (this.paramsObj.workItemId) {
          // 已办 B 代办A
          this.attachmentInfo.btnConfig = this.paramsObj.type === 'done' ? 'B' : 'A'
        } else {
          // 综合查询旧页面
          this.attachmentInfo.btnConfig = 'B'
          this.attachmentInfo.pageSite = 'attachmentInfo'
        }
      }
    },
    // 获取从综合查询进来的adjustid
    async getInitOnlyId() {
      const { code, data } = await getOnlyId(this.paramsObj.prjId)
      if (+code === 200) {
        this.isTag = true
        this.paramsObj.adjustId = data?.adjId
        this.getAttachmentInfo()
      }
    },
    // 页面初始化获取processInid 流程节点 以及浏览器地址数据
    async getBalanceInfo() {
      // 浏览器地址 获取参数
      const obj = getQueryObject()
      this.paramsObj.workItemId = obj?.workItemId
      this.paramsObj.prjId = obj?.prjId
      this.paramsObj.adjustId = obj?.adjustId
      this.paramsObj.processInstId = obj?.processInstId
      this.paramsObj.type = obj?.type
      // 已办done 代办todo 存在workItemId  综合查询prjid 没有type
      if (!this.paramsObj.workItemId) {
        this.isShowType = false
        this.getInitOnlyId()
      } else {
        if (this.paramsObj.type === 'done') {
          this.isShowType = false
        }
        const { workItemId } = this.paramsObj
        const { code, data } = await getWFWorkItem({ workItemId: workItemId })
        if (+code === 200) {
          this.isTag = true
          this.paramsObj.processInstId = data.processInstID.toString()
          this.attachmentInfo.nodeId = data.activityDefID
          this.paramsObj.prjId = data.bizObject.prjId
          this.paramsObj.adjustId = data.bizObject?.bizSeq
          this.getAttachmentInfo()
        } else {
          throw new Error('获取流程信息失败')
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped></style>
